﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace XDCCManager.Controls
{
	/// <summary>
	/// Interaction logic for ServerList.xaml
	/// </summary>
	public partial class ServerList : UserControl
	{
		public ServerList()
		{
			InitializeComponent();
		}

		public static readonly DependencyProperty ShowCurrentProperty = DependencyProperty.Register("ShowCurrent", typeof(bool), typeof(ServerList));

		public bool ShowCurrent
		{
			get { return (bool)GetValue(ServerList.ShowCurrentProperty); }
			set { SetValue(ServerList.ShowCurrentProperty, value); }
		}

		public static readonly DependencyProperty ShowCloseProperty = DependencyProperty.Register("ShowClose", typeof(bool), typeof(ServerList));

		public bool ShowClose
		{
			get { return (bool)GetValue(ServerList.ShowCloseProperty); }
			set { SetValue(ServerList.ShowCloseProperty, value); }
		}

		public XDCCManager.Popups.ServerEditor Editor
		{
			get { return (XDCCManager.Popups.ServerEditor)GetValue(EditorProperty); }
			set { SetValue(EditorProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Editor.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty EditorProperty =
			DependencyProperty.Register("Editor", typeof(XDCCManager.Popups.ServerEditor), typeof(ServerList), new UIPropertyMetadata(null, new PropertyChangedCallback((@do, args) =>
		{
			XDCCManager.Popups.ServerEditor editor = args.OldValue as XDCCManager.Popups.ServerEditor;
			if (editor != null)
			{
				editor.Ok -= (@do as ServerList).Editor_Ok;
				editor.Cancel -= (@do as ServerList).Editor_Cancel;
				editor.Remove -= (@do as ServerList).Editor_Remove;
			}
			editor = args.NewValue as XDCCManager.Popups.ServerEditor;
			if (editor != null)
			{
				editor.Ok += (@do as ServerList).Editor_Ok;
				editor.Cancel += (@do as ServerList).Editor_Cancel;
				editor.Remove += (@do as ServerList).Editor_Remove;
			}
		})));

		private void Editor_Remove(object sender, EventArgs e)
		{
			XDCCManager.Model.ServerContext serverContext = FindResource("ServerContextDataSource") as XDCCManager.Model.ServerContext;
			Editor.Visibility = System.Windows.Visibility.Collapsed;
			if (modifiedServer != null)
			{
				serverContext.Servers.Remove(modifiedServer);
			}
			modifiedServer = null;
			newServer = null;
		}

		private void Editor_Ok(object sender, XDCCManager.EventHandlers.ServerEventArgs e)
		{
			XDCCManager.Model.ServerContext serverContext = FindResource("ServerContextDataSource") as XDCCManager.Model.ServerContext;
			Editor.Visibility = System.Windows.Visibility.Collapsed;
			if (modifiedServer != null)
			{
				if (!modifiedServer.Equals(newServer))
				{
					int index = serverContext.Servers.IndexOf(modifiedServer);
					serverContext.Servers.Remove(modifiedServer);
					serverContext.Servers.Insert(index, newServer);
				}
			}
			else
			{
				serverContext.Servers.Add(newServer);
			}
			modifiedServer = null;
			newServer = null;
		}

		private void Editor_Cancel(object sender, EventArgs e)
		{
			Editor.Visibility = System.Windows.Visibility.Collapsed;
			modifiedServer = null;
			newServer = null;
		}

		private void Edit_Click(object sender, RoutedEventArgs e)
		{
			if (Editor != null)
			{
				modifiedServer = FindServerItem(e.Source as DependencyObject);
				if (modifiedServer != null)
				{
					newServer = modifiedServer.Clone() as Model.Servers.Server;
					Editor.DataContext = newServer;
					Editor.Visibility = System.Windows.Visibility.Visible;
					Editor.ShowRemove = true;
				}
			}
		}

		private void Connect_Click(object sender, RoutedEventArgs e)
		{
			Model.Servers.Server server = FindServerItem(e.Source as DependencyObject);
			if ((server != null) && (Connect != null))
				Connect(this, new EventHandlers.ServerEventArgs(server));
		}

		private void Close_Click(object sender, RoutedEventArgs e)
		{
			if (Close != null)
				Close(this, new EventArgs());
		}

		private void Add_Click(object sender, RoutedEventArgs e)
		{
			if (Editor != null)
			{
				modifiedServer = null;
				newServer = new Model.Servers.Server();
				Editor.DataContext = newServer;
				Editor.Visibility = System.Windows.Visibility.Visible;
				Editor.ShowRemove = false;
			}
		}

		private Model.Servers.Server FindServerItem(DependencyObject element)
		{
			while ((element != null) && (!(element is ListBoxItem)))
			{
				element = VisualTreeHelper.GetParent(element);
			}
			if (element == null)
				return null;
			return (element as FrameworkElement).DataContext as Model.Servers.Server;
		}

		public event EventHandler<XDCCManager.EventHandlers.ServerEventArgs> Connect;
		public event EventHandler Close;
		private Model.Servers.Server modifiedServer;
		private Model.Servers.Server newServer;
	}
}
